package listener;

import java.util.HashSet;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

import domain.Student;
import domain.Teacher;


/**
 * 监听用户在Session添加属性，填入ServletContext中，用于获取在线的老师和同学
 * 
 * May 15, 2009
 *
 * @author Fabian
 *
 */
public class SessionUserListener implements  HttpSessionAttributeListener {

	/* (non-Javadoc)
	 * @see javax.servlet.http.HttpSessionAttributeListener#attributeAdded(javax.servlet.http.HttpSessionBindingEvent)
	 * Session添加属性的处理
	 */
	public void attributeAdded(HttpSessionBindingEvent event) {
		
		HashSet<Integer> stuSet = null;
		HashSet<Integer> tchSet = null;
		
		//先获取ServletContext中用于存放在线用户的列表Set（学生和老师）
		ServletContext context = event.getSession().getServletContext();
		
		if (context.getAttribute("studentSet") == null) {		//没有就创建
			stuSet = new HashSet<Integer>();
			context.setAttribute("studentSet", stuSet);
		}
		if (context.getAttribute("teacherSet") == null) {		//没有就创建
			tchSet = new HashSet<Integer>();
			context.setAttribute("teacherSet", tchSet);
		}
		
		//获取session中添加的属性，如果是user，则加入ServletContent中
		String attrName = event.getName();
		if ("user".equals(attrName)) {
			Object user = event.getValue();
			if (user instanceof Student) {		//如果是学生
				Student stu = (Student)user;
				int id = stu.getId();
				stuSet = (HashSet<Integer>)context.getAttribute("studentSet");
				if (stuSet != null) {
					stuSet.add(id);
				}
				context.removeAttribute("studentSet");
				context.setAttribute("studentSet", stuSet);
			} else if (user instanceof Teacher) {	//如果是老师
				Teacher tch = (Teacher)user;
				int id = tch.getId();
				tchSet = (HashSet<Integer>)context.getAttribute("teacherSet");
				if (tchSet != null) {
					tchSet.add(id);
				}
				context.removeAttribute("teacherSet");
				context.setAttribute("teacherSet", tchSet);
			}
		}		
	}

	/* (non-Javadoc)
	 * @see javax.servlet.http.HttpSessionAttributeListener#attributeRemoved(javax.servlet.http.HttpSessionBindingEvent)
	 * Session删除属性的处理
	 */
	public void attributeRemoved(HttpSessionBindingEvent event) {
		
		HashSet<Integer> stuSet = null;
		HashSet<Integer> tchSet = null;
		
		//先获取ServletContext中用于存放在线用户的列表Set（学生和老师）
		ServletContext context = event.getSession().getServletContext();
		
		Object studentSet = context.getAttribute("studentSet");
		Object teacherSet = context.getAttribute("teacherSet");
		
		if (studentSet == null) {		//没有就创建
			stuSet = new HashSet<Integer>();
			context.setAttribute("studentSet", stuSet);
		}
		if (teacherSet == null) {		//没有就创建
			tchSet = new HashSet<Integer>();
			context.setAttribute("teacherSet", tchSet);
		}
		
		//获取session中添加的属性，如果是user，则加入ServletContent中
		String attrName = event.getName();
		if ("user".equals(attrName)) {
			Object user = event.getValue();
			if (user instanceof Student) {		//如果是学生
				Student stu = (Student)user;
				int id = stu.getId();				
				stuSet = (HashSet<Integer>)context.getAttribute("studentSet");
				if (stuSet != null) {
					stuSet.remove(id);
				}
				context.removeAttribute("studentSet");
				context.setAttribute("studentSet", stuSet);
			} else if (user instanceof Teacher) {	//如果是老师
				Teacher tch = (Teacher)user;
				int id = tch.getId();
				tchSet = (HashSet<Integer>)context.getAttribute("teacherSet");
				if (tchSet != null) {
					tchSet.remove(id);
				}
				context.removeAttribute("teacherSet");
				context.setAttribute("teacherSet", tchSet);
			}
		}				
	}

	/* (non-Javadoc)
	 * @see javax.servlet.http.HttpSessionAttributeListener#attributeReplaced(javax.servlet.http.HttpSessionBindingEvent)
	 */
	public void attributeReplaced(HttpSessionBindingEvent arg0) {		
	}


}
